//
//  LYSchemeManager.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/8/3.
//

import UIKit

enum SchemeJumpType: String, RawRepresentable {
    case main = "acren"
    case login = "travelitude"
    case order = "ploral"
    case continuing = "great"
}

class LYSchemeManager: NSObject {
    let scheme = "antics://micade.fractrecently.wondership/"
    let tabKey = "andform"
    let productIdKey = "caulithoughose"
    static let shared = LYSchemeManager()
    override init() {
        super.init()
    }

    func parser(_ url: String) {
        guard let lastPath = URL(string: url)?.lastPathComponent,
              let components = URLComponents(string: url),
              let jumpType = SchemeJumpType(rawValue: lastPath),
              let topVC = UIApplication.topViewController() else { return }
        switch jumpType {
        case .main:
            topVC.navigationController?.popToRootViewController(animated: true)
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                if let mainVC = UIApplication.shared.currentKeyWindow?.rootViewController as? LYMainViewController {
                    mainVC.changeViewController(to: 0)
                    
                    if let vc = mainVC.children.first,
                       let homeVC = (vc as? LYNavigationController)?.viewControllers.first as? LYEmohViewController {
                        homeVC.updateData()
                    }
                }
            }
        case .login:
            NotificationCenter.default.post(name: .NotLoginInNotificationKey, object: nil)
        case .order:
            topVC.navigationController?.popToRootViewController(animated: true)
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                if let mainVC = UIApplication.shared.currentKeyWindow?.rootViewController as? LYMainViewController {
                    mainVC.changeViewController(to: 1)
                    if let vc = mainVC.children.first,
                       let orderVC = (vc as? LYNavigationController)?.viewControllers.first as? LYOrderViewController {
                        if let paramters = self.parameters(queryItems: components.queryItems) {
                            if let tab = paramters[self.tabKey] as? String {
                                orderVC.updateData(tab: tab)
                            }
                        }
                    }
                }
            }
        case .continuing:
            let productDetailVC = LYProductDetailViewController()
            if let paramters = self.parameters(queryItems: components.queryItems) {
                
                if let productId = paramters[self.productIdKey] as? String {
                    productDetailVC.productId = Int(productId)
                }
            }
            topVC.navigationController?.pushViewController(productDetailVC, animated: true)
        }
    }
    
    func parameters(queryItems: [URLQueryItem]?) -> [String: Any]? {
        guard let queryItems = queryItems, !queryItems.isEmpty else { return nil }
        
        var result = [String: Any]()
        for item in queryItems {
            // 处理多值参数（相同key的情况）
            if let existingValue = result[item.name] {
                if var values = existingValue as? [String] {
                    if let newValue = item.value {
                        values.append(newValue)
                        result[item.name] = values
                    }
                } else if let singleValue = existingValue as? String, let newValue = item.value {
                    result[item.name] = [singleValue, newValue]
                }
            } else {
                result[item.name] = item.value ?? NSNull()
            }
        }
        return result
    }
}


